package com.tdam2012.grupo04.messageWeb;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;

public class ServerConnection {
	
	private static String url =  "http://172.16.170.7:8080/MessageSender/Sender";
	
	public boolean parseLogin(String user) throws ClientProtocolException, IOException, SAXException{
		String xml= 
				"<action id='REQUEST_RANDOM_VALUE' name='register-user'>" +
					"<action-detail>" +
						"<user username='" + user + "' password='123456' />" +
					"</action-detail>" +
				"</action>";
		try{
				HttpClient client = new DefaultHttpClient();
				HttpPost post = new HttpPost(url);
				BasicHttpEntity ent = new BasicHttpEntity();
				ent.setContentType("application/xml");
		
				ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
				ent.setContent(in);
				post.setEntity(ent);						
				HttpResponse responsePost= client.execute(post);
				HttpEntity resEntity= responsePost.getEntity();
				
				if(resEntity == null) return false;
				
				SAXParser parser = XmlParserProvider.getParser();
				Handler handler = new Handler();
						
				String res= EntityUtils.toString(resEntity);
				parser.parse(new ByteArrayInputStream(res.getBytes()), handler);
				XmlParserProvider.releaseParser(parser);
				return handler.isOK();
		}
		catch(Exception e){
			return false;
		}
		
	}
	
	public boolean parseSend(String user, String destination, String message) throws ClientProtocolException, IOException, SAXException{
		String xml= "<action id='REQUEST_RANDOM_VALUE' name='send-message'>" +
						"<action-detail>" +
							"<auth username='" + user + "' key='123456'></auth>" +
								"<message to='" + destination + "'>" +
								"<![CDATA[" + message + "]]></message>" +
						"</action-detail>" +
					"</action>";
		try	{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			BasicHttpEntity ent = new BasicHttpEntity();
			ent.setContentType("application/xml");
		
			ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
			ent.setContent(in);
			post.setEntity(ent);						
			HttpResponse responsePost= client.execute(post);
			HttpEntity resEntity= responsePost.getEntity();
								
			if(resEntity == null) return false;
		
			SAXParser parser = XmlParserProvider.getParser();
			Handler handler = new Handler();
						
			String res= EntityUtils.toString(resEntity);
			parser.parse(new ByteArrayInputStream(res.getBytes()), handler);
			XmlParserProvider.releaseParser(parser);
			return handler.isOK();
		}
		catch(Exception e){
			return false;
		}
	}
	
	public List<MessageWeb> parseGet(String user, String timestamp) throws ClientProtocolException, IOException, SAXException{
		String xml= "<action id='REQUEST_RANDOM_VALUE' name='get-messages'>" +
						"<action-detail>" +
							"<auth username='" + user + "' key='123456'></auth>" +
							"<filter type='timestamp'>" + timestamp + "</filter>" +
						"</action-detail>" +
					"</action>";
		try	{
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(url);
			BasicHttpEntity ent = new BasicHttpEntity();
			ent.setContentType("application/xml");
		
			ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
			ent.setContent(in);
			post.setEntity(ent);						
			HttpResponse responsePost= client.execute(post);
			HttpEntity resEntity= responsePost.getEntity();
								
			if(resEntity == null) return null;
		
			SAXParser parser = XmlParserProvider.getParser();
			HandlerReciver handler = new HandlerReciver();
						
			String res= EntityUtils.toString(resEntity);
			parser.parse(new ByteArrayInputStream(res.getBytes()), handler);
			XmlParserProvider.releaseParser(parser);
			return handler.getMensajes();
		}
		catch(Exception e){
			return null;
		}
	}
	
}


